<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar"> </div>
      <!-- 登录表单区域 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-width="0px" class="loginForm">

        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click.prevent="submitForm">登录</el-button>
          <el-button type="info" @click.prevent="resetForm">重置</el-button>
        </el-form-item>

      </el-form>


    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        rules: {
          // 用户名验证规则
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 20, message: '用户名长度应为3到20个字符', trigger: 'blur' }
          ],
          // 密码验证规则
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 30, message: '密码长度应为6到30个字符', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submitForm() {
        this.$refs.loginFormRef.validate(async valid => {
          if (!valid) return
          const { data: result } = await this.$http.post('login', this.loginForm)
          if (result.meta.status !== 200) return this.$message.error('登录失败')
          this.$message.success('登录成功')
          // 1.将登录成功之后的token保存到客户端的sessionStorage中
          window.sessionStorage.setItem('token', result.data.token)
          // 2.通过编程式导航跳转到后台主页
          this.$router.push('/home')

        });
      },
      resetForm() {
        this.$refs.loginFormRef.resetFields()
      }
    },

  }
</script>
<style lang="less" scoped>
  .login_container {
    background-color: rgb(90, 119, 119);
    height: 100vh;
  }

  .login_box {
    width: 500px;
    height: 400px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .avatar {
    position: absolute;
    left: 50%;
    width: 200px;
    height: 200px;
    transform: translate(-50%, -50%);
    background-image: url("../assets/logo.png");
    border: 10px solid white;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
  }

  .loginForm {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
</style>